package com.snjx.netty.chat.netty.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName MyChannelInitializer
 * @Author yunlong.zhang
 * @Date 2018/11/13
 * @since: JDK 1.8
 * @Description //TODO
 **/
public class MyChannelInitializer  extends ChannelInitializer {

    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast("http-codec", new HttpServerCodec()); // 将请求和应答消息解码为HTTP消息
        pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); // 将HTTP消息的多个部分合成一条完整的HTTP消息
        pipeline.addLast("http-chunked", new ChunkedWriteHandler()); // 向客户端发送HTML5文件
        pipeline.addLast(new IdleStateHandler(60, 30, 60 * 30, TimeUnit.SECONDS)); //进行设置心跳检测
        pipeline.addLast("http-chunked", new ChunkedWriteHandler()); // WebSocket通信支持
        pipeline.addLast(new MyChannelHandler()); // 配置通道处理  来进行业务处理
    }
}
